package com.example.sb.advice;

import com.example.sb.validator.UserValidator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

/**
 * @author kedong.lin
 */
@ControllerAdvice(basePackages = "com.example.sb.controller.*",annotations = Controller.class)
public class MyControllerAdvice {
    @InitBinder
    public void initBinder(WebDataBinder binder){
        binder.setValidator(new UserValidator());
    }
    @ModelAttribute
    public void modelAttribute(Model model){
        model.addAttribute("testN","11");
    }
    @ExceptionHandler(value = Exception.class)
    public void exception(Model model,Exception e){
        model.addAttribute("exceptingMsg",e.getMessage());
    }
}
